package com.neulogistics.contrib.warehouse.api.application.command.handler;

import com.blackto.lib.mediat.core.Mediator;
import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.contrib.warehouse.api.application.command.InboundCommand;
import com.neulogistics.contrib.warehouse.api.application.command.PurchaseInboundCommand;
import com.neulogistics.contrib.warehouse.api.application.command.ReturnInboundCommand;
import com.neulogistics.contrib.warehouse.api.application.command.TransferInboundCommand;
import com.neulogistics.contrib.warehouse.api.application.integrationevent.WarehouseIntegrationEventService;
import com.neulogistics.contrib.warehouse.api.application.integrationevent.publish.PurchaseInboundIntegrationEvent;
import com.neulogistics.contrib.warehouse.api.application.integrationevent.publish.SubstationReturnInboundIntegrationEvent;
import com.neulogistics.contrib.warehouse.api.application.integrationevent.publish.TransferInboundIntegrationEvent;
import com.neulogistics.contrib.warehouse.api.application.rpc.IWarehouseRPC;
import com.neulogistics.contrib.warehouse.api.service.ThreadLocalService;
import com.neulogistics.contrib.warehouse.domain.exception.WarehouseDomainException;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.CirculationOrder;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.Inventory;
import com.neulogistics.contrib.warehouse.domain.model.aggregate.Warehouse;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationDetail;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationStatus;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationType;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.WarehouseLevel;
import com.neulogistics.contrib.warehouse.domain.model.valueobject.GoodsItem;
import com.neulogistics.contrib.warehouse.domain.repository.ICirculationOrderRepository;
import com.neulogistics.contrib.warehouse.domain.repository.IInventoryRepository;
import com.neulogistics.contrib.warehouse.domain.repository.IWarehouseRepository;
import com.neulogistics.infrastructure.cache.idservice.RedisIdUtil;
import com.neulogistics.infrastructure.common.annotation.ProduceEvent;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import com.neulogistics.infrastructure.integrationeventlog.model.IntegrationEventConstant;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

@Component
public class InboundCommandHandler implements IRequestHandler<InboundCommand, BaseResponse<Boolean>> {
    @Resource
    ICirculationOrderRepository circulationOrderRepository;

    @Resource
    Mediator mediator;

    @Override
    public BaseResponse<Boolean> handle(InboundCommand command) throws WarehouseDomainException {
        // 根据入库单id查询对应实体
        CirculationOrder circulationOrder = circulationOrderRepository.findById(command.getCirculationId());

        //校验出入库单状态
        circulationOrder.checkCirculationTypeAndDetail(CirculationType.Inbound);

        Boolean result = Boolean.FALSE;
        CirculationDetail circulationDetail = circulationOrder.getCirculationDetail();
        // 调拨入库
        if(circulationDetail == CirculationDetail.Transfer_Inbound){
            TransferInboundCommand transferInboundCommand =
                    new TransferInboundCommand(command.circulationId, command.warehouseId);

            Boolean success = mediator.send(transferInboundCommand);
            result = success == null ?
                    Boolean.FALSE : success;
        }
        // 退货入库
        else if(circulationDetail == CirculationDetail.Return_Inbound){
            ReturnInboundCommand returnInboundCommand =
                    new ReturnInboundCommand(command.circulationId, command.warehouseId);

            Boolean success = mediator.send(returnInboundCommand);
            result = success == null ?
                    Boolean.FALSE : success;
        }
        // 采购入库
        else if(circulationDetail == CirculationDetail.Purchase_Inbound){
            PurchaseInboundCommand purchaseInboundCommand =
                    new PurchaseInboundCommand(command.circulationId, command.warehouseId);

            Boolean success = mediator.send(purchaseInboundCommand);
            result = success == null ?
                    Boolean.FALSE : success;
        }
        return ResultUtils.success(result);
    }
}
